import Foundation

//可选类型
var optionalName: String? = "John"
//允许赋值为null
optionalName = nil
print(String(describing: optionalName))


var name1: String? = "Alice"
print(name1!) // 强制解包，输出：Alice

var name2: String? = nil
// print(name2!) // 强制解包，运行时错误：unexpectedly found nil while unwrapping an Optional value


// 可选绑定
var name3: String? = "Bob"
if let unwrappedName = name3{
    print("Hello, \(unwrappedName)") // 输出：Hello, Bob
} else {
    print("Name is nil")
}

func checkName() {
    var name4: String? = "Alice"
    guard let unwrappedName1 = name4 else {
        print("Name is nil")
        return
    }
    print("Hello, \(unwrappedName1)")
}

checkName()